package com.system.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.am.base.BaseDao;
import com.am.ui.tag.EhCacheManager;
import com.system.uitls.CommonUtil;
import com.system.uitls.IdUtils;
import com.system.vo.DataDictionary;

public class DictionaryDao extends BaseDao
{
  public void updateDictionaryCache()
  {
    Map cache = new HashMap();

    String sql = "SELECT id,code FROM sys_data_dictionary WHERE id IN (SELECT pId FROM sys_data_dictionary)";
    List<Map<String, Object>> list = getSimpleJdbc().queryForList(sql, new Object[0]);

    for (Map m : list)
    {
      sql = "SELECT id,code,value FROM sys_data_dictionary WHERE pId=?";
      List<Map<String, Object>> children = getSimpleJdbc().queryForList(sql, new Object[] { m.get("id") });

      List obj = new ArrayList();
      for (Map map : children)
      {
        obj.add(
          new DataDictionary(CommonUtil.nullToEmptyString(map.get("id")), 
          CommonUtil.nullToEmptyString(map.get("code")), 
          CommonUtil.nullToEmptyString(map.get("value"))));
      }

      cache.put(CommonUtil.nullToEmptyString(m.get("code")), obj);
    }

    EhCacheManager.remove("constantsDateCache", "dataDictionaryKey");

    EhCacheManager.put("constantsDateCache", "dataDictionaryKey", cache);
  }

  public List<Map<String, Object>> getDictionaryList()
  {
    String sql = "SELECT id,pId,value as name FROM sys_data_dictionary";
    return getSimpleJdbc().queryForList(sql, new Object[0]);
  }

  public Map<String, Object> getDictionary(String id)
  {
    String sql = "SELECT id,code,value,remark FROM sys_data_dictionary WHERE id=?";
    return getSimpleJdbc().queryForMap(sql, new Object[] { id });
  }

  public String findRoot()
  {
    String sql = "SELECT id FROM sys_data_dictionary where pId is null";
    return (String)getSimpleJdbc().queryForObject(sql, String.class, new Object[0]);
  }

  public Map<String, String> delete(String id)
  {
    Map result = new HashMap();
    try {
      String sql = "DELETE FROM sys_data_dictionary WHERE id=? or pId=?";
      getSimpleJdbc().update(sql, new Object[] { id, id });

      updateDictionaryCache();
      result.put("type", "success");
      result.put("value", "删除成功");
    }
    catch (Exception ex) {
      result.put("value", "删除失败");
    }

    return result;
  }

  public Map<String, Object> update(Map<String, Object> map)
  {
    Map result = new HashMap();
    String id = CommonUtil.nullToEmptyString(map.get("id"));
    String pId = CommonUtil.nullToEmptyString(map.get("pId"));
    String code = CommonUtil.nullToEmptyString(map.get("code"));
    String value = CommonUtil.nullToEmptyString(map.get("value"));
    String remark = CommonUtil.nullToEmptyString(map.get("remark"));

    String sql = "";
    if ("".equals(id))
      try
      {
        id = IdUtils.getSequence(getJdbc(), "sys_data_dictionary_id");
        sql = "INSERT INTO sys_data_dictionary(id,pId,code,value,remark) VALUES(?,?,?,?,?)";
        getSimpleJdbc().update(sql, new Object[] { id, pId, code, value, remark });
        updateDictionaryCache();
        result.put("id", id);
        result.put("oprt", "save");
        result.put("value", "保存成功");
      }
      catch (Exception ex) {
        ex.printStackTrace();
        result.put("value", "保存失败");
      }
    else {
      try
      {
        sql = "UPDATE sys_data_dictionary set code=?,value=?,remark=? where id=?";
        getSimpleJdbc().update(sql, new Object[] { code, value, remark, id });
        updateDictionaryCache();
        result.put("oprt", "edit");
        result.put("value", "修改成功");
      }
      catch (Exception ex) {
        result.put("value", "修改失败");
      }
    }

    return result;
  }

  public Map<String, Object> updateMessage(Map<String, Object> map)
  {
    Map result = new HashMap();
    String operater1 = CommonUtil.nullToEmptyString(map.get("operater1"));
    String operater1Num = CommonUtil.nullToEmptyString(map.get("operater1Num"));
    String operater2 = CommonUtil.nullToEmptyString(map.get("operater2"));
    String operater2Num = CommonUtil.nullToEmptyString(map.get("operater2Num"));
    String responsibility = CommonUtil.nullToEmptyString(map.get("responsibility"));
    String date1 = CommonUtil.nullToEmptyString(map.get("date1"));
    String date2 = CommonUtil.nullToEmptyString(map.get("date2"));
    String date3 = CommonUtil.nullToEmptyString(map.get("date3"));
    String date4 = CommonUtil.nullToEmptyString(map.get("date4"));
    String notice = CommonUtil.nullToEmptyString(map.get("notice"));

    String sql = "";
    try {
      sql = "UPDATE sys_config SET operater1=?,operater1Num=?,operater2=?,operater2Num=?,responsibility=?,date1=?,date2=?,date3=?,date4=?,notice=? WHERE id=?";
      getSimpleJdbc().update(sql, new Object[] { operater1, operater1Num, operater2, operater2Num, responsibility, date1, date2, date3, date4, notice, "1" });
      result.put("value", "保存成功");
    }
    catch (Exception ex) {
      ex.printStackTrace();
      result.put("value", "保存失败");
    }

    return result;
  }

  public Map<String, Object> getMessage() {
    Map result = null;
    String sql = null;
    try {
      sql = "SELECT operater1,operater1Num,operater2,operater2Num,responsibility,date1,date2,date3,date4,notice FROM sys_config WHERE id=?";
      result = getSimpleJdbc().queryForMap(sql, new Object[] { "1" });
    }
    catch (Exception ex) {
      logger.info("DictionaryDao[getMessage] sql:" + sql + "\n" + ex.getMessage());
    }

    return result;
  }
}